<?php

class CreditsController extends AppController {

    var $name = 'Credits';
    var $paginate = array(
        'limit' => '10',
        'order' => array('Credit.id' => 'desc')
    );

    function beforeFilter() {
        $this->Auth->loginRedirect = array('controller' => 'Users', 'action' => 'admin_index');
        //$this->Auth->allow('check');
    }

    function admin_index() {
        if (!$this->Access->check('User', 'read')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller' => 'Users', 'action' => 'index'));
        }else{
            $data = $this->paginate('Credit');
            $this->set('credits',$data);
        }
        
    }

    function admin_add() {
        if (!$this->Access->check('User', 'create')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller' => 'Users', 'action' => 'index'));
        } else {

            if (!empty($this->data)) {
                if ($this->Credit->save($this->data)) {
                    $this->Session->setFlash('Credit saved');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                } else {
                    $this->Session->setFlash('Somthing Went wrong. Credit not saved');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                }
            }
        }
    }

    function admin_edit($id=null) {
        if (!$this->Access->check('User', 'update')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller' => 'Users', 'action' => 'index'));
        }else{
            if(empty($this->data)){
                $this->data=$this->Credit->read(null,$id);
            }else{
                if($this->Credit->save($this->data)){
                    $this->Session->setFlash('Credit Updated');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                }else{
                    $this->Session->setFlash('Somthing went wrong. Credit not saved');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                }
            }
        }
    }

    function admin_delete($id=null) {
        if (!$this->Access->check('User', 'delete')) {
            $this->Session->setFlash('You dont have permissions to View this content');
            $this->redirect(array('controller' => 'Users', 'action' => 'index'));
        }else{
            if(!empty($id)){
                if($this->Credit->delete($id)){
                    $this->Session->setFlash('Credit Deleted');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                }else{
                    $this->Session->setFlash('Invalid Id for credit');
                    $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
                }
            }else{
                $this->Session->setFlash('Somthing went wrong. Credit not deleted');
                $this->redirect(array('controller' => 'Credits', 'action' => 'index'));
            }
        }
        
    }

}

?>
